import QtQuick
import QtQuick.Layouts
import QtCharts
import Qt5Compat.GraphicalEffects

Rectangle {
    Layout.fillWidth: true
    Layout.fillHeight: true

    PolarChartView {
        id: chart
        title: "雷达扫描示例"
        titleFont.bold: true
        titleFont.pointSize: 14

        anchors.fill: parent
        legend.visible: false
        antialiasing: true
        backgroundColor: "transparent"

        ValueAxis {
            id: axisAngular
            min: 0
            max: 360
            tickCount: 9
        }

        ValueAxis {
            id: axisRadial
            min: 0
            max: 1.0
            tickCount: 6
        }

        // 基础散点（暗色，作为背景目标）
        ScatterSeries {
            id: seriesBase
            axisAngular: axisAngular
            axisRadial: axisRadial
            markerSize: 6
            color: "#44A0B3C5"
            borderColor: "transparent"
            markerShape: ScatterSeries.MarkerShapeCircle
        }

        // 扫描高亮散点（被扫描扇形覆盖时高亮）
        ScatterSeries {
            id: seriesHighlight
            axisAngular: axisAngular
            axisRadial: axisRadial
            markerSize: 8
            color: "#00FF88"
            borderColor: "transparent"
            markerShape: ScatterSeries.MarkerShapeCircle
        }

        property var targets: [] // {a: angle(0..360), r: radius(0..1)}
        property real sweepAngle: 0
        property real sweepWidth: 8 // 度

        Component.onCompleted: {
            // 生成随机目标
            const count = 20
            for (let i = 0; i < count; i++) {
                const a = Math.random() * 360
                const r = Math.pow(Math.random(), 0.6) // 更靠近外圈一些
                targets.push({ a: a, r: r })
                seriesBase.append(a, r)
            }
            scanTimer.start()
        }

        Timer {
            id: scanTimer
            interval: 16
            repeat: true
            running: false
            onTriggered: {
                chart.sweepAngle = (chart.sweepAngle + 0.8) % 360

                // 更新高亮点：落在扫描扇形内的点
                const half = chart.sweepWidth * 0.5
                for (let i = 0; i < chart.targets.length; i++) {
                    const t = chart.targets[i]
                    // 环状角度差（0..180）
                    const diff = Math.abs(((t.a - chart.sweepAngle + 540) % 360) - 180)
                    if (diff <= half) {
                        seriesHighlight.append(t.a, t.r)
                    }
                }
            }
        }
    }

    // 扫描扇形叠加层（与图表 plotArea 对齐）
    Rectangle {
        x: chart.plotArea.x
        y: chart.plotArea.y
        width: chart.plotArea.width
        height: chart.plotArea.height
        color: "transparent"

        ConicalGradient {
            anchors.fill: parent
            // 以圆心为中心
            source: null
            gradient: Gradient {
                // 创建窄带扫描 + 前后渐隐
                GradientStop { position: 0.00; color: "#00000000" }
                GradientStop { position: 0.45; color: "#00000000" }
                GradientStop { position: 0.48; color: "#2200FF88" }
                GradientStop { position: 0.50; color: "#8800FF88" }
                GradientStop { position: 0.52; color: "#2200FF88" }
                GradientStop { position: 0.55; color: "#00000000" }
                GradientStop { position: 1.00; color: "#00000000" }
            }
            angle: (chart.sweepAngle + 180) % 360
            horizontalOffset: 0.5
            verticalOffset: 0.5
        }
    }
}
